package com.technology.education.ui.common.register

import android.os.Bundle
import com.technology.education.data.AppData
import com.technology.education.data.SpKeys
import com.technology.education.databinding.ActivityRegisterBinding
import com.technology.education.ext.saveData
import com.technology.education.ext.toast
import com.technology.education.ui.common.information.CompleteInfoActivity
import com.technology.education.ui.market.MainMarketActivity
import com.technology.education.ui.student.MainStudentActivity
import com.technology.education.ui.teacher.MainTeacherActivity
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity
import me.hgj.jetpackmvvm.ext.lifecycle.KtxActivityManger
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.util.get
import me.hgj.jetpackmvvm.util.startActivity

/**
 *@author:create by BZY
 *Date: 2022/2/24 17
 */
class ActivityRegister : BaseVmDbActivity<RegisterViewModel, ActivityRegisterBinding>() {
    override fun initView(savedInstanceState: Bundle?) {
        mViewModel.type.set(intent.get("type", 0) ?: 0)
        mDatabind.vm = mViewModel
        mDatabind.toolBar.tvTitle.text = when (mViewModel.type.get()) {
            0 -> "注册账号"
            1 -> "忘记密码"
            2 -> "修改密码"
            else -> "注册账号"
        }
        mDatabind.textview.text = when (mViewModel.type.get()) {
            0 -> "注册账号"
            1 -> "忘记密码"
            2 -> "修改密码"
            else -> "注册账号"
        }

        mDatabind.button.text = when (mViewModel.type.get()) {
            0 -> "注册"
            1 -> "确定"
            2 -> "修改"
            else -> "确定"
        }

        mDatabind.toolBar.toolbar.setNavigationOnClickListener {
            onBackPressed()
        }
    }

    override fun showLoading(message: String) {
    }

    override fun dismissLoading() {
    }

    override fun createObserver() {

        mViewModel.loginResult.observe(this)
        {
            parseState(it, onSuccess = { info ->
                AppData.TOKEN = info.token
                saveData(SpKeys.TOKEN, info.token)
                saveData(SpKeys.MEMBER_TYPE, AppData.MEMBER_TYPE)
                if (info.name == null) {
                    this.startActivity(CompleteInfoActivity::class)
                } else {
                    saveData(SpKeys.IS_LOGIN, true)
                    KtxActivityManger.finishAllActivity()
                    when (AppData.MEMBER_TYPE) {
                        "0" -> this.startActivity(MainStudentActivity::class)
                        "1" -> this.startActivity(MainTeacherActivity::class)
                        "2" -> this.startActivity(MainMarketActivity::class)
                    }
                }
                onBackPressed()
            }, onError = { ex ->
                toast(ex.errorMsg)
            })

        }
    }
}